
#include <iostream>
#include <string>
#include <vector>
#include <assert.h>
using namespace std;

void test_string1(void)
{
	string s1;
	string s2("this is a log");
	//string s3(&s2);
	string s3(s2);
	string s4 = s2;
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3<<endl;
	cout << s4 << endl;

	string s5("this is a log",6);
	cout << s5 << endl;
	string s5a(s2, 6);
	cout << s5a << endl;

	string s6(10, 'w');
	cout << s6 << endl;

	string s7(s2, 6, 3);
	cout << s7 << endl;
	string s8(s2, 6, 30);
	cout << s8 << endl;
	string s10(s2, 6);
	cout << s10 << endl;
}

void test_string2(void)
{
	string s1("what is that");
	string s2("that is a pig");
	cout << s1 << endl;
	s1 = s2;
	cout << s1 << endl;
	s1 = "dog";
	cout << s1 << endl;
	s1 = 'p';
	cout << s1 << endl;
}

void test_string3(void)
{
	string s1("hello,world");
	cout << s1[0] << endl;
	s1[0] = 'o';
	cout << s1.size() << endl;
	cout << s1.length() << endl;

	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i]<<" ";
	}
	cout << endl;

	const char* ss = "pollo";
	cout << ss[2] << endl;

	string::iterator it1 = s1.begin();
	while (it1 < s1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;

	for (auto ch:s1)
	{
		cout << ch << " ";
	}
	cout << endl;

	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);

	vector<int>::iterator itv = v1.begin();
	while (itv < v1.end())
	{
		cout << *itv << " ";
		itv++;
	}
	cout << endl;
}

namespace bit
{
	class string1
	{
	public:
		string& operator[](size_t pos)
		{
			assert(pos < _capacity);
			_str[pos] = 'p';
			return _str[pos];
		}
	private:
		string* _str;
		size_t _size;
		size_t _capacity;
	};
}

void test_string4(void)
{
	string s1("hello,world");
	const string s2("hello,world");
	cout << s1.at(6) << endl;
	cout << s2.at(6) << endl;
	cout << s1[6] << endl;
	cout << s2[6] << endl;
	s1[0] = 'x';
	cout << s1[0] << endl;
	s1.at(0) = 'y';
	cout << s1[0] << endl;
	//s2[0] = 'x';
	//s2.at(6) = 'y';
}

void func(const string& s1)
{
	string::const_iterator it1 = s1.begin();
	while (it1 < s1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;

	string::const_reverse_iterator it1_r = s1.rbegin();
	while (it1_r != s1.rend())
	{
		cout << *it1_r << " ";
		it1_r++;
	}
	cout << endl;
}

void test_string5(void)
{
	string s("hello,china");
	//func(s);
	string::iterator it1 = s.begin();
	while (it1 != s.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
	cout << s << endl;

	string::reverse_iterator it1_r = s.rbegin();
	while (it1_r < s.rend())
	{
		cout << *it1_r << " ";
		it1_r++;
	}
	cout << endl;
	cout << s << endl;
}

void test_string6(void)
{
	//string s("hello,world");
	//cout << s.length() << endl;
	//cout << s.size() << endl;
	//cout << s.max_size() << endl;
	//cout << s.capacity() << endl;

	//string s1;
	//s1.reserve(1000);
	//s1.resize(1000, 'x');
	//cout << sizeof(s1) <<" "<<s1.capacity() << endl;
	//string s2("I like C");
	//cout << s2 << endl;
	//s2.resize(s2.size()+2, '+');
	//cout << s2 << endl;

	//string s3;
	//s3.reserve(1000);
	//s3.resize(1000, 'x');
	//size_t sz = s3.capacity();
	////cout << "making s3 grow:\n";
	////cout << "capacity changed: " << sz << endl;
	//for (int i = 0; i < 1000; ++i)
	//{
	//	s3.push_back('c');
	//	if (sz != s3.capacity())
	//	{
	//		sz = s3.capacity();
	//		cout << "capacity changed:" << sz << endl;
	//	}
	//}

	string ss;
	ss.push_back('x');
	ss.append("hello");
	string sstr("world");
	ss.append(sstr);
	cout << ss << endl;

	string ss1;
	ss1 += 'x';
	ss1 += "hello";
	ss1 += sstr;
	cout << ss1 << endl;
}

int main()
{
	//test_string1();
	//test_string2();
	//test_string3();
	//test_string4();
	//test_string5();
	test_string6();

	return 0;
}